package com.shareach.util;

import java.util.Calendar;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shareach.redis.RedisService;


@Service("tokenAction")
public class TokenAction {
	@Autowired
	private RedisService redisService;
	
	RedisStore redisStore = new RedisStore(); 
	
	
	/**
	 * 生成driver Token
	 * @param userId 用户数字Id
	 * @return
	 */
	public String makeToken(long userId){
		String uid = HelperUtil.idFromNumber(userId,8);
		return makeToken(uid);
	}
	
	/**
	 * 生成driver Token
	 * @param uid 用户字符串Id
	 * @return
	 */
	public String makeToken(String uid){
		String token = DigestUtils.md5Hex(uid+Calendar.getInstance().getTimeInMillis());
		redisStore.Save(uid, token);
		return token;
	}
	
	/**
	 * 检查 driver Token
	 * @param userId 用户数字Id
	 * @param token 令牌值
	 * @return
	 */
	public boolean checkToken(long userId, String token){
		String uid = HelperUtil.idFromNumber(userId,8);
		return checkToken(uid,token);
	}
	
	/**
	 * 检查driver Token
	 * @param uid 用户字符串Id
	 * @param token 令牌值
	 * @return
	 */
	public boolean checkToken(String uid, String token){
		String tokenStored = redisStore.getToken(uid);
		return token.equals(tokenStored);
	}
	
	/**
	 * 移除driver Token
	 * @param token 令牌值
	 * @return
	 */
	public void removeToken(String token){
		redisStore.removeByToken(token);
	}
	
	/**
	 * 移除driver Token
	 * @param uid 字符串Id
	 * @return
	 */
	public void removeTokenByUerId(String uid){
		redisStore.removeByUid(uid);
	}
	
	/**
	 * 移除driver Token
	 * @param userId 数字Id
	 * @return
	 */
	public void removeTokenByUerId(long userId){
		String uid = HelperUtil.idFromNumber(userId,8);
		redisStore.removeByUid(uid);
	}
	
	/**
	 * 根据 driver Token取得UID
	 * @param token 令牌值
	 * @return
	 */
	public String getUid(String token){
		return redisStore.getUid(token);
	}
	
	/**
	 * 根据driver Uid取得Token
	 * @param token 令牌值
	 * @return
	 */
	public String getToken(String uid){
		return redisStore.getToken(uid);
	}
	
	/**
	 * 根据driver UserId取得token
	 * @param userId
	 * @return
	 */
	public String getToken(long userId){
		String uid = HelperUtil.idFromNumber(userId,8);
		return getToken(uid);
	}

	/**
	 * Redis操作类
	 * @author Shareach
	 */
	class RedisStore{
		private static final String Token_UID="usr:token";
		/**
		 * 保存driver Token和Uid
		 * @param uid
		 * @param token
		 */
		public void Save(String uid, String token){
		    //清除已经存在的token
			removeByUid(uid);
			if(uid!=null)
				redisService.getRedis().setHashValue(Token_UID, token, uid);
			if(token!=null)
				redisService.getRedis().setHashValue(Token_UID, uid, token);
		}
		
		/**
		 * 根据driver Uid取得Token
		 * @param uid
		 * @return
		 */
		public String getToken(String uid){
			return redisService.getRedis().getHashValue(Token_UID, uid);
		}
		
		/**
		 * 根据driver Token取得Uid
		 * @param token
		 * @return
		 */
		public String getUid(String token){
			return redisService.getRedis().getHashValue(Token_UID, token);
		}
		
		/**
		 * 根据driver Uid删除缓存
		 * @param uid
		 * @return
		 */
		public void removeByUid(String uid){
			String token = redisService.getRedis().getHashValue(Token_UID, uid);
			redisService.getRedis().delHashValue(Token_UID, uid);
			if(token!=null)	
				redisService.getRedis().delHashValue(Token_UID, token);
		}
		
		/**
		 * 根据driver Token删除缓存
		 * @param token
		 * @return
		 */
		public void removeByToken(String token){
			String uid = redisService.getRedis().getHashValue(Token_UID, token);
			if(uid!=null)
				redisService.getRedis().delHashValue(Token_UID, uid);
			redisService.getRedis().delHashValue(Token_UID, token);
		}
	}
}
